package com.kanesoft.android.gdx.widgets;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class Button extends GdxWidgets {

    public enum ButtonFormat {
        SQUARE, RECTANGLE, CIRCLE, STAR, ROUNDED_RECTANGLE
    };

    public enum ButtonColor {
        RED, MAGENTA, CIAN, LIGHT_GREEN, GREEN, YELLOW, ORANGE, BLACK
    };

    private final String BTN_FILE_NAME = "data/buttons_mask.png";

    private final float SEC_PER_FRAME = 1f;
    private float frameTime;

    private final int BTN_IMG_HEIGHT = 58;
    private final int[] BTN_IMG_WIDTH = { 60, 160, 54, 54, 207 };
    private final int[] BTN_IMG_TYPE_START_POS = { 0, 76, 250, 320, 391 };
    private final int[] BTN_IMG_COLOR_START_POS = { 0, 58, 58 * 2, 58 * 3,
            58 * 4, 58 * 5, 58 * 6, (58 * 7)+2 };

    private int btnPosX;
    private int btnPosY;
    private int btnWidth;
    private int btnHeight;
    
    private ButtonFormat format;
    private ButtonColor color;

    private Texture btnTexture;
    private SpriteBatch spriteBatch;
    private Animation btnAnimation;
    private TextureRegion btnFrame;
    
    private WidgetEvents btnEventsHandler;

    public Button(int x, int y, int width, int height, ButtonFormat format, ButtonColor color) {
        this(x, y, width, height, format, color, null);

    }
    
    public Button(int x, int y, int width, int height, ButtonFormat format, ButtonColor color, WidgetEvents eventsHandler) {
        super(eventsHandler);
        if (width <= 0) width = 5;
        if (height <= 0) height = 5;
        
        btnPosX = x;
        btnPosY = y;
        btnWidth = width;
        btnHeight = height;
        
        this.format = format;
        this.color = color;

        btnTexture = new Texture(Gdx.files.internal(BTN_FILE_NAME));
        btnAnimation = new Animation(SEC_PER_FRAME, getTexture(), getTextureClick());
        spriteBatch = new SpriteBatch();
    }
    


    private TextureRegion getTexture() {

        int imagePosX0 = BTN_IMG_TYPE_START_POS[format.ordinal()];
        int imagePosY0 = BTN_IMG_COLOR_START_POS[color.ordinal()];
        int imageWidth = BTN_IMG_WIDTH[format.ordinal()];
        int imageHeight = BTN_IMG_HEIGHT;

        return new TextureRegion(btnTexture, imagePosX0, imagePosY0, imageWidth, imageHeight);
    }
    
    private TextureRegion getTextureClick() {

        int imagePosX0 = BTN_IMG_TYPE_START_POS[format.ordinal()];
        int imagePosY0 = BTN_IMG_COLOR_START_POS[2];
        int imageWidth = BTN_IMG_WIDTH[format.ordinal()];
        int imageHeight = BTN_IMG_HEIGHT;

        return new TextureRegion(btnTexture, imagePosX0, imagePosY0, imageWidth, imageHeight);
    }

    @Override
    public void draw(float dletaTime, float offsetX, float offsetY) {
        spriteBatch.begin();
        btnFrame = btnAnimation.getKeyFrame(frameTime, true);
        spriteBatch.draw(btnFrame, btnPosX+offsetX, btnPosY+offsetY, btnWidth, btnHeight);
        spriteBatch.end();
    }
    
    @Override 
    public void onPressDown(int x, int y) {
        
        if ((x < btnPosX) || (y < btnPosY) || (x > btnPosX+btnWidth) || (y > btnPosY+btnHeight)) return;
        
        System.out.println("onTouchEvent(" + x + "," + y + ")");
        frameTime = 1;
    }
    
    @Override 
    public void onPressUp(int x, int y) {
        frameTime = 0;
        
        if ((x < btnPosX) || (y < btnPosY) || (x > btnPosX+btnWidth) || (y > btnPosY+btnHeight)) return;
        
        if (btnEventsHandler == null) return;
        
        btnEventsHandler.onClick();
        
    }

    @Override
    public float[] getWidgetVertices() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public boolean isWidgetOverlapping(GdxWidgets widget) {
        // TODO Auto-generated method stub
        return false;
    }

}
